package xyz.mumu233.robot.listenner;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.forte.qqrobot.anno.Filter;
import com.forte.qqrobot.anno.Listen;
import com.forte.qqrobot.anno.depend.AllBeans;
import com.forte.qqrobot.anno.depend.Beans;
import com.forte.qqrobot.beans.messages.msgget.GroupMsg;
import com.forte.qqrobot.beans.messages.types.MsgGetTypes;
import com.forte.qqrobot.beans.types.KeywordMatchType;
import com.forte.qqrobot.sender.MsgSender;
import com.forte.qqrobot.utils.CQCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import xyz.mumu233.robot.api.FeiFei;
import xyz.mumu233.robot.api.XiaoI;
import xyz.mumu233.robot.bean.Image;
import xyz.mumu233.robot.bean.ScheduleJob;
import xyz.mumu233.robot.bean.api.feifei.FeiFeiResult;
import xyz.mumu233.robot.bean.api.xiaoi.XiaoIResult;
import xyz.mumu233.robot.enums.MasterStatusEnum;
import xyz.mumu233.robot.enums.PropertiesEnum;
import xyz.mumu233.robot.mapper.ImageMapper;
import xyz.mumu233.robot.mapper.ScheduleJobMapper;
import xyz.mumu233.robot.service.ScheduleJobService;
import xyz.mumu233.robot.util.ConfigUtil;
import xyz.mumu233.robot.util.SpringContextUtil;

import java.util.List;

/**
 * @Author mumu
 * @create 2019/4/27 12:23
 */
@Beans
@Listen(MsgGetTypes.groupMsg)
public class MyMsgGroupListener {

    private ImageMapper mapper = SpringContextUtil.getBean(ImageMapper.class);

    @Filter(at = true)
    public void onAtMessage(GroupMsg msg,MsgSender sender, CQCodeUtil cqCodeUtil){
        //不在线自动回复
        if(!(ConfigUtil.statusEnum == MasterStatusEnum.ONLINE)){
            QueryWrapper<Image> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("image_key","自闭");
            Image image = mapper.selectOne(queryWrapper);
            sender.SENDER.sendGroupMsg(msg.getGroup(),cqCodeUtil.getCQCode_image(image.getImagePath())
                    + cqCodeUtil.getCQCode_at(msg.getQQ())+
                    " 你好，我正在"+ConfigUtil.statusEnum.getContent()+",有什么事情等会说啊");
        }
        //TODO
    }

    @Filter(value = "\\？[\\u4e00-\\u9fa5]*",keywordMatchType = KeywordMatchType.REGEX)
    public void onMessage(GroupMsg msg,MsgSender sender, CQCodeUtil cqCodeUtil){
        String aiMsg = msg.getMsg().replace("？","");
        FeiFeiResult feiFeiResult = FeiFei.getReply(aiMsg);
        sender.SENDER.sendGroupMsg(msg.getGroup(),feiFeiResult.getContent());
    }


    @Filter(value = "获取任务列表",keywordMatchType = KeywordMatchType.REGEX)
    public void test(GroupMsg msg,MsgSender sender){
        if(msg.getGroup().equals(ConfigUtil.properties.getProperty(PropertiesEnum.MANAGEGROUPIP.getKey()))){
            ScheduleJobService scheduleJobService = SpringContextUtil.getBean(ScheduleJobService.class);
            ScheduleJobMapper scheduleJobMapper = SpringContextUtil.getBean(ScheduleJobMapper.class);
            List<ScheduleJob> jobs = scheduleJobMapper.selectList(null);
            StringBuilder builder = new StringBuilder();
            for(ScheduleJob job : jobs){
                builder.append(job.getId()+" "+job.getJobName()+" "+job.getStatus()+"\n");
            }
            sender.SENDER.sendGroupMsg(msg.getGroup(),builder.toString());
        }
    }

    @Filter(value = "获取状态列表",keywordMatchType = KeywordMatchType.REGEX)
    public void getStatusList(GroupMsg msg,MsgSender sender){
        if(msg.getGroup().equals(ConfigUtil.properties.getProperty(PropertiesEnum.MANAGEGROUPIP.getKey()))){
            StringBuilder send = new StringBuilder();
            for(MasterStatusEnum masterStatusEnum : MasterStatusEnum.values()){
                send.append(masterStatusEnum.getCode()+" | "+masterStatusEnum.getContent()+"\n");
            }
            sender.SENDER.sendGroupMsg(msg.getGroup(),send.toString());

        }
    }

    @Filter(value = "设置状态 \\d",keywordMatchType = KeywordMatchType.REGEX)
    public void setStatus(GroupMsg msg,MsgSender sender){
        if(msg.getGroup().equals(ConfigUtil.properties.getProperty(PropertiesEnum.MANAGEGROUPIP.getKey()))){
            String[] config = msg.getMsg().split(" ");
            ConfigUtil.statusEnum = MasterStatusEnum.getStatusByCode(Integer.valueOf(config[1]));
            sender.SENDER.sendGroupMsg(msg.getGroup(),"已设置状态为："+
                    MasterStatusEnum.getStatusByCode(Integer.valueOf(config[1])).getContent());
        }

    }

    @Filter(value = "获取状态",keywordMatchType = KeywordMatchType.REGEX)
    public void getStatus(GroupMsg msg,MsgSender sender){
        if(msg.getGroup().equals(ConfigUtil.properties.getProperty(PropertiesEnum.MANAGEGROUPIP.getKey()))){
            StringBuilder send = new StringBuilder();
            send.append("状态："+ConfigUtil.statusEnum.getContent()+"\n");
            sender.SENDER.sendGroupMsg(msg.getGroup(),send.toString());
        }
    }

}
